//
//  GYMineShopAddressEditVC.m
//  GYDemo
//
//  Created by mac on 2018/6/20.
//  Copyright © 2018年 mac. All rights reserved.
//

//
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                  \\`.
//        .'//                     |                     \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-. \\`.
//    .'//.-"                 `-.  |  .-'                 "-.\\`.
//  .'//______.============-..   \ | /   ..-============.______\\`.
//.'______________________________\|/______________________________`.
//
//

#import "GYMineShopAddressEditVC.h"
#import "Head.h"

@interface GYMineShopAddressEditVC ()<ACEExpandableTableViewDelegate>
@property (nonatomic,strong) UIButton *saveBtn;
@property (nonatomic,strong) UIButton *defaultSignIV;
@property (nonatomic,strong) UILabel *defaultSignLB;
@end

@implementation GYMineShopAddressEditVC
#pragma mark - getter and setter
-(GYMineShopAddressEditViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [GYMineShopAddressEditViewModel new];
    }
    return _viewModel;
}

-(UIButton *)defaultSignIV
{
    if (!_defaultSignIV) {
        _defaultSignIV = [UIButton new];
        _defaultSignIV.frame = WFCGRectMake(114, 528, 16, 16);
        _defaultSignIV.y = self.saveBtn.y - WFCGFloatY(25);
        [_defaultSignIV setEnlargeEdge:10];
        [_defaultSignIV addTarget:self action:@selector(defaultSignIVTap:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _defaultSignIV;
}

-(UILabel *)defaultSignLB
{
    if (!_defaultSignLB) {
        _defaultSignLB = [UILabel new];
        _defaultSignLB.frame = WFCGRectMake(141, 527, 150, 18);
        _defaultSignLB.y = self.saveBtn.y - WFCGFloatY(25);
        _defaultSignLB.font = SYSTEMFONT(13);
        _defaultSignLB.textColor = Main_Color_Black;
        _defaultSignLB.text = @"设置为默认收货地址";
    }
    return _defaultSignLB;
}

-(UIButton *)saveBtn
{
    if (!_saveBtn) {
        _saveBtn = [UIButton new];
        _saveBtn.x = 0;
        _saveBtn.width = self.view.width;
        _saveBtn.height = WFCGFloatY(50);
        _saveBtn.y = WF_SCREEN_HEIGHT - self.topShelterHeight - self.bottomShelterHeight - _saveBtn.height;
        _saveBtn.titleLabel.font = SYSTEMFONT(15);
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setBackgroundColor:Main_Color_Red forState:UIControlStateNormal];
        [_saveBtn setTitleColor:Main_Color_White forState:UIControlStateNormal];
        [_saveBtn addTarget:self action:@selector(saveBtnTap:) forControlEvents:UIControlEventTouchUpInside];
        _saveBtn.layer.borderColor = Main_Color_White.CGColor;
        _saveBtn.layer.borderWidth = 4;
    }
    return _saveBtn;
}
#pragma mark - event
-(void)saveBtnTap:(id)sender
{
    [self.viewModel requestSave];
}
-(void)defaultSignIVTap:(id)sender
{
    if (self.viewModel.detailModel.name) {
        if ([self.viewModel.detailModel.defaultflag isEqualToString:@"1"]) {
            [self.viewModel requestSetDefault];
        }
    }else{
        if (self.viewModel.listViewModel.datas.count > 0) {
            self.viewModel.updateModel.defaultflag = [self.viewModel.updateModel.defaultflag isEqualToString:@"1"] ? @"0" : @"1";
            [self reloadView];
        }
    }
}
#pragma mark - method
-(void)initilization
{
    self.navigationItem.title = self.viewModel.indexPath ? @"修改收货地址" : @"添加收货地址";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.navigationController.navigationBar.shadowImage = [[UIImage new]rt_darkenWithLevel:0.3f];
    [self setNavgationTheme:1];
    [self.view addSubview:self.baseTable];
    [self.baseTable setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    [self.baseTable setSeparatorColor: Main_Color_BG];
    [self.baseTable setBackgroundColor:Main_Color_BG];
    [self.baseTable setTintColor:Main_Color_BG];
    self.baseTable.height = self.saveBtn.y;
    if (@available(iOS 11.0, *)) {
        //        self.baseTable.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);
    }
    self.baseTable.mj_header.backgroundColor = Main_Color_Red;
    self.baseTable.scrollIndicatorInsets = self.baseTable.contentInset;
    UIView *foot = [UIView new];
    foot.height = self.saveBtn.height;
    [self.baseTable setTableFooterView:foot];
//    [self.view addSubview:self.defaultSignIV];
//    [self.view addSubview:self.defaultSignLB];
    [self.view addSubview:self.saveBtn];
    [self viewModelBand];
    [self reloadData];
}
-(void)viewModelBand
{
    @WeakSelf;
    GCBaseObservationModel *observation = [GCBaseObservationModel new];
    observation.observation = self.viewModel;
    observation.keyPath = @"updateSign";
    observation.handler = ^(NSString *keyPath) {
        [selfp.navigationController popViewControllerAnimated:YES];
    };
    [self registObservation:observation];
    
    GCBaseObservationModel *observation1 = [GCBaseObservationModel new];
    observation1.observation = self.viewModel;
    observation1.keyPath = @"items";
    observation1.handler = ^(NSString *keyPath) {
        [selfp reloadView];
    };
    [self registObservation:observation1];
}
-(void)reloadView
{
    NSString *defaultflag = self.viewModel.updateModel.defaultflag ? self.viewModel.updateModel.defaultflag : @"1";
    [_defaultSignIV setImage:[UIImage imageNamed:1 == defaultflag.integerValue ? @"bty" : @"Checkbox"] forState:UIControlStateNormal];
    [self.baseTable reloadData];
}
-(void)reloadData
{
    [self.viewModel requestData];
}
///选择区域
-(void)chooseStartArea:(GCBaseEditCellViewModel *)cellmodel
{
    @WeakSelf;
    [BRAddressPickerView showAddressPickerWithShowType:BRAddressPickerModeArea dataSource:[WFFunctions getLocalAddressList] defaultSelected:nil isAutoSelect:nil themeColor:Main_Color_Black resultBlock:^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
        
        NSString *areas = [NSString stringWithFormat:@"%@%@%@",province.name,city.name,area.name];
        cellmodel.value = areas;
        selfp.viewModel.updateModel.province = province.name;
        selfp.viewModel.updateModel.city = city.name;
        selfp.viewModel.updateModel.area = area.name;
        [selfp.baseTable reloadData];
    } cancelBlock:^{
        
    }];
}
#pragma mark - life
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initilization];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.items.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GCBaseEditCellViewModel *cellmodel = self.viewModel.items[indexPath.row];
    return cellmodel.cellHeight;;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellID = @"GYMineShopAddressEditVCCellID";
    GCBaseEditCellViewModel *cellmodel = self.viewModel.items[indexPath.row];
    GYExpandableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
    if (!cell) {
        cell = [[GYExpandableCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.expandableTableView = tableView;
    }
    cell.accessoryType = cellmodel.canEdit ? UITableViewCellAccessoryNone : UITableViewCellAccessoryDisclosureIndicator;
    NSString *titles = cellmodel.unit ? [NSString stringWithFormat:@"%@(%@)",cellmodel.title,cellmodel.unit] : cellmodel.title;
    if (cellmodel.necessary) {
        NSString *title = [NSString stringWithFormat:@"* %@",titles];
        NSMutableAttributedString *mastr = [[NSMutableAttributedString alloc]initWithString:title];
        [mastr addAttribute:NSFontAttributeName value:cell.titleLB.font range:[title rangeOfString:title]];
        [mastr addAttribute:NSForegroundColorAttributeName value:Main_Color_Red range:[title rangeOfString:@"*"]];
        cell.titleLB.attributedText = mastr;
    }else{
        cell.titleLB.text = titles;
    }
    cell.textView.text = cellmodel.value;
    cell.textView.zw_placeHolderColor = Main_Color_LightGray;
    cell.textView.zw_placeHolder = cellmodel.placeholder;
    CGFloat cellheight = [cell cellHeight];
    cellmodel.cellHeight = cellheight;
    cell.textView.editable = cellmodel.canEdit;
    cell.textView.userInteractionEnabled = cell.textView.editable;
    cell.textView.keyboardType = cellmodel.inputType;
    return cell;
}
-(void)tableView:(UITableView* )tableView willDisplayCell:(UITableViewCell* )cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cell setSeparatorInset:UIEdgeInsetsMake(0, WFCGFloatX(13), 0, 0)];
    [cell setLayoutMargins:UIEdgeInsetsZero];
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    view.backgroundColor = Main_Color_BG;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GCBaseEditCellViewModel *cellmodel = self.viewModel.items[indexPath.row];
    NSString *action = cellmodel.tapAction;
    if (action && [self.viewModel.cellActions containsObject:action]) {
        SEL selector = NSSelectorFromString(action);
        [self performSelector:selector withObject:cellmodel];
    }
}
#pragma mark - ace delegate
- (void)tableView:(UITableView *)tableView updatedText:(NSString *)text atIndexPath:(NSIndexPath *)indexPath {
    GCBaseEditCellViewModel *cellmodel = self.viewModel.items[indexPath.row];
    NSString *key = cellmodel.modelKey;
    cellmodel.value = text;
    [self.viewModel.updateModel setValue:text forKey:key];
}
-(void)tableView:(UITableView *)tableView updatedHeight:(CGFloat)height atIndexPath:(NSIndexPath *)indexPath
{
    GCBaseEditCellViewModel *cellmodel = self.viewModel.items[indexPath.row];
    cellmodel.cellHeight = height;
}
@end


/*
 11111111111111111111111111111111111111001111111111111111111111111
 11111111111111111111111111111111111100011111111111111111111111111
 11111111111111111111111111111111100001111111111111111111111111111
 11111111111111111111111111111110000111111111111111111111111111111
 11111111111111111111111111111000000111111111111111111111111111111
 11111111111111111111111111100000011110001100000000000000011111111
 11111111111111111100000000000000000000000000000000011111111111111
 11111111111111110111000000000000000000000000000011111111111111111
 11111111111111111111111000000000000000000000000000000000111111111
 11111111111111111110000000000000000000000000000000111111111111111
 11111111111111111100011100000000000000000000000000000111111111111
 11111111111111100000110000000000011000000000000000000011111111111
 11111111111111000000000000000100111100000000000001100000111111111
 11111111110000000000000000001110111110000000000000111000011111111
 11111111000000000000000000011111111100000000000000011110001111111
 11111110000000011111111111111111111100000000000000001111100111111
 11111111000001111111111111111111110000000000000000001111111111111
 11111111110111111111111111111100000000000000000000000111111111111
 11111111111111110000000000000000000000000000000000000111111111111
 11111111111111111100000000000000000000000000001100000111111111111
 11111111111111000000000000000000000000000000111100000111111111111
 11111111111000000000000000000000000000000001111110000111111111111
 11111111100000000000000000000000000000001111111110000111111111111
 11111110000000000000000000000000000000111111111110000111111111111
 11111100000000000000000001110000001111111111111110001111111111111
 11111000000000000000011111111111111111111111111110011111111111111
 11110000000000000001111111111111111100111111111111111111111111111
 11100000000000000011111111111111111111100001111111111111111111111
 11100000000001000111111111111111111111111000001111111111111111111
 11000000000001100111111111111111111111111110000000111111111111111
 11000000000000111011111111111100011111000011100000001111111111111
 11000000000000011111111111111111000111110000000000000011111111111
 11000000000000000011111111111111000000000000000000000000111111111
 11001000000000000000001111111110000000000000000000000000001111111
 11100110000000000001111111110000000000000000111000000000000111111
 11110110000000000000000000000000000000000111111111110000000011111
 11111110000000000000000000000000000000001111111111111100000001111
 11111110000010000000000000000001100000000111011111111110000001111
 11111111000111110000000000000111110000000000111111111110110000111
 11111110001111111100010000000001111100000111111111111111110000111
 11111110001111111111111110000000111111100000000111111111111000111
 11111111001111111111111111111000000111111111111111111111111100011
 11111111101111111111111111111110000111111111111111111111111001111
 11111111111111111111111111111110001111111111111111111111100111111
 11111111111111111111111111111111001111111111111111111111001111111
 11111111111111111111111111111111100111111111111111111111111111111
 11111111111111111111111111111111110111111111111111111111111111111
 
 
 */
